angular.module('starter.services', [])

.factory('Chats', function() {
  // Might use a resource here that returns a JSON array

  // Some fake testing data
  var chats = [{
    id: 0,
    name: 'Ben Sparrow',
    lastText: 'You on your way?',
    face: 'img/ben.png'
  }, {
    id: 1,
    name: 'Max Lynx',
    lastText: 'Hey, it\'s me',
    face: 'img/max.png'
  }, {
    id: 2,
    name: 'Adam Bradleyson',
    lastText: 'I should buy a boat',
    face: 'img/adam.jpg'
  }, {
    id: 3,
    name: 'Perry Governor',
    lastText: 'Look at my mukluks!',
    face: 'img/perry.png'
  }, {
    id: 4,
    name: 'Mike Harrington',
    lastText: 'This is wicked good ice cream.',
    face: 'img/mike.png'
  }];

  return {
    all: function() {
      return chats;
    },
    remove: function(chat) {
      chats.splice(chats.indexOf(chat), 1);
    },
    get: function(chatId) {
      for (var i = 0; i < chats.length; i++) {
        if (chats[i].id === parseInt(chatId)) {
          return chats[i];
        }
      }
      return null;
    }
  };
})

.factory('meilvFac', function ($resource,cfg) {
    // Service logic
    // ...

    // Public API here
    return {
      homepage: $resource(cfg.baseUrl + 'home/homepage/:action', {}, {
        'index': {method:'GET', params:{action:'index'},isArray:false}
      }),
      articleshow: $resource(cfg.baseUrl + 'home/articleshow/:action', {}, {
        'index': {method:'GET', params:{action:'index'},isArray:false}
      }),
      holiday: $resource(cfg.baseUrl + 'home/holiday/:action', {}, {
        'holidayget': {method:'GET', params:{action:'holidayget'},isArray:false}
      }),
      wechat: $resource(cfg.baseUrl + 'home/wechat/:action', {}, {
        'signpackage': {method:'GET', params:{action:'signpackage'},isArray:false}
      }),
      order: $resource(cfg.baseUrl + 'home/order/:action', {}, {
        'orderget': {method:'GET', params:{action:'orderget'},isArray:false},
        'wxpay': {method:'POST', params:{action:'wxpay'},isArray:false},
        'cancel': {method:'POST', params:{action:'cancel'},isArray:false},
        'delete': {method:'POST', params:{action:'delete'},isArray:false}
      }),
      oneday: $resource(cfg.baseUrl + 'home/oneday/:action', {}, {
        'orderAdd': {method:'POST', params:{action:'orderAdd'},isArray:false}
      }),
      user: $resource(cfg.baseUrl + 'home/user/:action', {}, {
        'userinfoget': {method:'GET', params:{action:'userinfoget'},isArray:false},
        'userinfoedit': {method:'POST', params:{action:'userinfoedit'},isArray:false},
        'sendsms': {method:'POST', params:{action:'sendsms'},isArray:false}
      }),
      collection: $resource(cfg.baseUrl + 'home/collection/:action', {}, {
        'collectionadd': {method:'POST', params:{action:'collectionadd'},isArray:false},
        'collectionget': {method:'GET', params:{action:'collectionget'},isArray:false}
      }),
      disney: $resource(cfg.baseUrl + 'home/disney/:action', {}, {
        'disneyholidayget': {method:'GET', params:{action:'disneyholidayget'},isArray:false},
        'disneypriceget': {method:'GET', params:{action:'disneypriceget'},isArray:false},
        'orderadd': {method:'POST', params:{action:'orderadd'},isArray:false}
      }),
      feedback: $resource(cfg.baseUrl + 'home/feedback/:action', {}, {
        'addfeedback': {method:'POST', params:{action:'addfeedback'},isArray:false}
      })
    };
  })


.service('timeString', function(){
  //十位数时间戳转换成"yyyy-MM-dd HH:mm"格式和"yyyy-MM-dd"格式
  //"yyyy-MM-dd HH:mm"格式和"yyyy-MM-dd"格式 转换成十位数时间戳
  var year, month, date, hour, minute, second;
  var string = function(time){
    time = new Date(time * 1000);
    year = time.getFullYear();
    month = time.getMonth()+1<10 ? '0'+ (time.getMonth()+1): time.getMonth()+1;
    date = time.getDate()<10 ? '0'+ time.getDate(): time.getDate();
    hour = time.getHours()<10 ? '0'+ time.getHours(): time.getHours();
    minute = time.getMinutes()<10 ? '0'+ time.getMinutes(): time.getMinutes();
    second = time.getMinutes()<10 ? '0'+ time.getMinutes(): time.getMinutes();
  }
  return {
    toTimeString : function(time){
      string(time);
      var timeString = year + '-' + month + '-' + date + ' ' + hour + ':' + minute + ':00';
      return timeString;
    },
    toDateString : function(time){
      string(time);
      var dateString = year + '-' + month + '-' + date;
      return dateString;
    },
    toTimeNum : function(time){
      var timestring = time.replace(/-/g,"/");
      var timenum = new Date(timestring).getTime()/1000;
      return timenum;
    }
  }
});
